import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()

def solid_geometry_fixed():
    fig = plt.figure(figsize=(15, 5))
    
    # 方法1: 使用plot_surface绘制立方体
    ax1 = fig.add_subplot(131, projection='3d')
    
    # 定义立方体的六个面
    # 底面 (z=0)
    x_bottom = np.array([[0, 1], [0, 1]])
    y_bottom = np.array([[0, 0], [1, 1]])
    z_bottom = np.array([[0, 0], [0, 0]])
    
    # 顶面 (z=1)
    x_top = np.array([[0, 1], [0, 1]])
    y_top = np.array([[0, 0], [1, 1]])
    z_top = np.array([[1, 1], [1, 1]])
    
    # 前面 (y=0)
    x_front = np.array([[0, 1], [0, 1]])
    y_front = np.array([[0, 0], [0, 0]])
    z_front = np.array([[0, 0], [1, 1]])
    
    # 后面 (y=1)
    x_back = np.array([[0, 1], [0, 1]])
    y_back = np.array([[1, 1], [1, 1]])
    z_back = np.array([[0, 0], [1, 1]])
    
    # 左面 (x=0)
    x_left = np.array([[0, 0], [0, 0]])
    y_left = np.array([[0, 1], [0, 1]])
    z_left = np.array([[0, 0], [1, 1]])
    
    # 右面 (x=1)
    x_right = np.array([[1, 1], [1, 1]])
    y_right = np.array([[0, 1], [0, 1]])
    z_right = np.array([[0, 0], [1, 1]])
    
    # 绘制所有面
    ax1.plot_surface(x_bottom, y_bottom, z_bottom, alpha=0.7, color='blue')
    ax1.plot_surface(x_top, y_top, z_top, alpha=0.7, color='blue')
    ax1.plot_surface(x_front, y_front, z_front, alpha=0.7, color='red')
    ax1.plot_surface(x_back, y_back, z_back, alpha=0.7, color='red')
    ax1.plot_surface(x_left, y_left, z_left, alpha=0.7, color='green')
    ax1.plot_surface(x_right, y_right, z_right, alpha=0.7, color='green')
    
    ax1.set_title('立方体')
    ax1.set_xlabel('X')
    ax1.set_ylabel('Y')
    ax1.set_zlabel('Z')
    ax1.set_xlim(0, 1)
    ax1.set_ylim(0, 1)
    ax1.set_zlim(0, 1)
    
    # 方法2: 使用voxels绘制立方体（更简单的方法）
    ax2 = fig.add_subplot(132, projection='3d')
    
    # 创建一个3D数组表示立方体
    cube = np.ones((2, 2, 2), dtype=bool)
    
    # 使用voxels绘制
    ax2.voxels(cube, alpha=0.7, edgecolor='k')
    ax2.set_title('立方体 (voxels方法)')
    ax2.set_xlabel('X')
    ax2.set_ylabel('Y')
    ax2.set_zlabel('Z')
    
    # 球体
    ax3 = fig.add_subplot(133, projection='3d')
    u = np.linspace(0, 2 * np.pi, 30)
    v = np.linspace(0, np.pi, 30)
    x = np.outer(np.cos(u), np.sin(v))
    y = np.outer(np.sin(u), np.sin(v))
    z = np.outer(np.ones(np.size(u)), np.cos(v))
    
    ax3.plot_surface(x, y, z, color='lightblue', alpha=0.7)
    ax3.set_title('球体')
    ax3.set_xlabel('X')
    ax3.set_ylabel('Y')
    ax3.set_zlabel('Z')
    
    plt.tight_layout()
    plt.show()

solid_geometry_fixed()